package com.example.yls.demoa;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.Date;

public class MainActivity extends AppCompatActivity {

    private Button startBtn;
    private Button stopBtn;
    private Button bindBtn;
    private Button unbindBtn;
    private Button btnAdd;
    private ServiceConnection serviceConnection;
    private MyBinder binder;
    private Button showTime;
    private Button reverseOrder;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        serviceConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
                Log.e("MainActivity","onServiceConnected");
                binder= (MyBinder) iBinder;
            }

            @Override
            public void onServiceDisconnected(ComponentName componentName) {
                    Log.e("MainActivity","onServiceDisconnected");
            }
        };
        startBtn= findViewById(R.id.start_btn);
        stopBtn=findViewById(R.id.stop_btn);
        bindBtn=findViewById(R.id.bind_service);
        unbindBtn=findViewById(R.id.unbind_service);
        btnAdd=findViewById(R.id.btn_add);
        showTime=findViewById(R.id.show_time);
        reverseOrder=findViewById(R.id.reverse_order);

        //显示当前时间
        showTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String time = binder.getTimeStr();
                Log.e("binder.getTimeStr","time="+time);
            }
        });
        //获得逆序字符
         reverseOrder.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View view) {
                 String str = binder.reverseStr("I Love Android");
                 Log.e(" binder.reverseStr","str="+str);
             }
         });

         //绑定服务
        bindBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent =new Intent();
                intent.setClass(MainActivity.this,MyService.class);
               bindService(intent,serviceConnection,BIND_AUTO_CREATE);
            }
        });

        //两个数相加
      btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int result=binder.add(100,200);
                Log.e("binder.add","result="+result);

                /*String str = binder.toUpperCase("hello world");
                Log.e("binder.toUpperCase","str="+str);
*/
            }
        });

      //取消绑定服务
        unbindBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                unbindService(serviceConnection);
            }
        });
        //启动服务
        startBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent =new Intent();
                intent.setClass(MainActivity.this,MyService.class);
                startService(intent);
            }
        });
        //停止服务
        stopBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent =new Intent();
                intent.setClass(MainActivity.this,MyService.class);
                stopService(intent);
            }
        });
    }
}
